package com.cyx.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyx.exam.entity.Class;
import com.cyx.exam.mapper.ClassMapper;
import com.cyx.exam.service.ClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cyx
 * @create 2021-03-22 16:40
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    @Resource
    private ClassMapper classMapper;

    @Override
    public IPage<Class> selectByPage(int start, int size) {
        Page<Class> page=new Page<>(start,size);
        classMapper.selectPage(page, null);
        return page;
    }

    @Override
    public IPage<Class> searchByPage(int start, int size, String name) {
        Page<Class> page=new Page<>(start,size);
        classMapper.selectPage(page, new QueryWrapper<Class>().like("name", "%"+name+"%"));
        return page;
    }

    @Override
    public int delete(Integer id) {
        QueryWrapper<Class> wrapper = new QueryWrapper<Class>();
        wrapper.eq("id",id);
        return classMapper.delete(wrapper);
    }

    @Override
    public List<String> getClassNameList() {
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        wrapper.select("name");
        List<Class> classes = classMapper.selectList(wrapper);
        return classes.stream().map(Class::getName).collect(Collectors.toList());// 取出其中一列
    }
}
